#include<iostream>
const int N = 6500;
int sum[N][N], n, f[N][N];
using namespace std;
int main()
{
	int n, m;cin >> n >> m;
	int x, y, v;
	for (int i = 1;i <= n; i++)
	{
			cin >> x >> y >> v;f[++x][++y] += v;
		
	}
	for (int i = 1; i <= 5001; i++)
	{
		for (int j = 1; j <= 5001; j++)
		{
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + f[i][j];
		}
	}
	int ret = 0, ans = 0;
	m = min(m, 5001);
	for (int x2 = m; x2 <= 5001; x2++)
	{
		for (int y2 = m; y2 <= 5001; y2++)
		{
			int x1 = x2 - m + 1;int y1 = y2 - m + 1;
			ret = max(ret, sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]);
		}
	}
	cout << ret << endl;
	return 0;
}